package de.betagames.dreamworld.charactereditor.model;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

public class Character {

	private String name, portraitName;
	private int _str, _int, _dex, _con, _dmg, _hp, _mana, _dodge;
	private BufferedImage portrait;

	private void calcDmg() {
		_dmg = _str * 100 / 10;
	}

	private void calcHP() {
		_hp = _con * 10;
	}

	private void calcMana() {
		_mana = _int * 10;
	}

	private void calcDodge() {
		_dodge = _dex * 100 / 10;
		if (_dodge > 50) {
			_dodge = 50;
		}
	}

	public String getDmg() {
		return _dmg + "%";
	}

	public String getDodge() {
		return _dodge + "%";
	}

	public int getHP() {
		return _hp;
	}

	public int getMana() {
		return _mana;
	}

	public int getStr() {
		return _str;
	}

	public int getInt() {
		return _int;
	}

	public int getDex() {
		return _dex;
	}

	public int getCon() {
		return _con;
	}

	public void raiseStr() {
		_str++;
		calcDmg();
	}

	public void lowerStr() {
		_str--;
		calcDmg();
	}

	public void raiseInt() {
		_int++;
		calcMana();
	}

	public void lowerInt() {
		_int--;
		calcMana();
	}

	public void raiseCon() {
		_con++;
		calcHP();
	}

	public void lowerCon() {
		_con--;
		calcHP();
	}

	public void raiseDex() {
		_dex++;
		calcDodge();
	}

	public void lowerDex() {
		_dex--;
		calcDodge();
	}

	public void setStr(int _str) {
		this._str = _str;
		calcDmg();
	}

	public void setInt(int _int) {
		this._int = _int;
		calcMana();
	}

	public void setCon(int _con) {
		this._con = _con;
		calcHP();
	}

	public void setDex(int _dex) {
		this._dex = _dex;
		calcDodge();
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPortrait(String title) {
		try {
			portraitName = title;
			if (!title.equalsIgnoreCase("default")) {
				portrait = ImageIO.read(new File(System.getProperty("user.dir")
						+ "/Portraits/" + title + ".png"));
			}
		} catch (IOException e) {
			portrait = null;
			portraitName = "default";
			JOptionPane
					.showMessageDialog(
							null,
							"<html>Your imagination level is too low, <br>"
									+ "to dream this picture.<br>"
									+ "You should try harder.<br>"
									+ "(Or possibly you should stop deleting images you need)</html>",
							"ERROR", JOptionPane.ERROR_MESSAGE, null);
		}
	}

	public void save() {

		try {
			File f = new File(System.getProperty("user.dir") + "/Characters/"
					+ name + ".char");

			FileWriter fw = new FileWriter(f);
			fw.write("name=" + name + "\r\n");
			fw.write("str=" + _str + "\r\n");
			fw.write("con=" + _con + "\r\n");
			fw.write("dex=" + _dex + "\r\n");
			fw.write("int=" + _int + "\r\n");
			fw.write("portrait=" + portraitName);
			fw.flush();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
